package com.idp.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.idp.app.dto.ScreenSySqhdDto;
import com.idp.app.dto.SqAjslDto;
import com.idp.common.enums.MessageEnum;
import com.idp.common.error.SystemException;
import com.idp.common.model.PageModel;
import com.idp.app.dto.SySqhdDto;
import com.idp.app.dto.SySqhdSearchDto;
import com.idp.app.entity.SySqhdEntity;
import com.idp.app.mapper.SySqhdMapper;
import com.idp.app.service.SySqhdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商圈划定Service实现类
 * @author Simon
 */
@Service("sySqhdService")
public class SySqhdServiceImpl implements SySqhdService {

	@Autowired
	private SySqhdMapper sySqhdMapper;

	@Override
	public PageModel<SySqhdDto> findByPage(SySqhdSearchDto sySqhdSearchDto) {

		PageModel<SySqhdDto> page = new PageModel<SySqhdDto>();

		page.setList(sySqhdMapper.getList(sySqhdSearchDto));

		return page;
	}

	@Override
	public SySqhdDto getById(Long id) {

		if(ObjectUtil.isEmpty(id)) {
			throw new SystemException(MessageEnum.PARAM_ERROR);
		}

		SySqhdDto sySqhdDto = sySqhdMapper.getById(id);

		if(sySqhdDto == null) {
			throw new SystemException(MessageEnum.DATA_NOT_EXIST);
		}

		return sySqhdDto;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(SySqhdDto sySqhdDto) {

		SySqhdEntity sySqhdEntity = BeanUtil.toBean(sySqhdDto, SySqhdEntity.class);

		sySqhdMapper.insert(sySqhdEntity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SySqhdDto sySqhdDto) {

		SySqhdEntity sySqhdEntity = sySqhdMapper.selectById(sySqhdDto.getId());

        if(sySqhdEntity == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }
		CopyOptions copyOptions = new CopyOptions();
		copyOptions.setIgnoreNullValue(true);
		BeanUtil.copyProperties(sySqhdDto, sySqhdEntity, copyOptions);

		sySqhdMapper.updateById(sySqhdEntity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(Long id) {

		SySqhdEntity sySqhdEntity = sySqhdMapper.selectById(id);

        if(sySqhdEntity == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }

		sySqhdMapper.deleteById(id);
	}

	@Override
	public List<SySqhdDto> getListByDd(String dd) {
		return sySqhdMapper.getListByDd(dd);
	}

	@Override
	public List<SqAjslDto> getSqAjslDtos(String dd) {
		return sySqhdMapper.getSqAjslDtos(dd);
	}

}
